﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Mogubang_Sl.Core;
using System.Globalization;
using System.Threading;
using OpenXLive;
using OpenXLive.Silverlight;
using OpenXLive.Features;

namespace Mogubang_Sl
{
    public partial class App : Application
    {
        /// <summary>
        ///提供对电话应用程序的根框架的轻松访问。
        /// </summary>
        /// <returns>电话应用程序的根框架。</returns>
        public PhoneApplicationFrame RootFrame { get; private set; }

        /// <summary>
        /// Application 对象的构造函数。
        /// </summary>
        public App()
        {
            // Global handler for uncaught exceptions. 
            UnhandledException += Application_UnhandledException;

            // Standard Silverlight initialization
            InitializeComponent();

            // 特定于电话的初始化
            InitializePhoneApplication();

            // 调试时显示图形分析信息。
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // 显示当前帧速率计数器。
                Application.Current.Host.Settings.EnableFrameRateCounter = true;

                // 显示在每个帧中重绘的应用程序区域。
                //Application.Current.Host.Settings.EnableRedrawRegions = true；

                // Enable non-production analysis visualization mode, 
                // 该模式显示递交给 GPU 的包含彩色重叠区的页面区域。
                //Application.Current.Host.Settings.EnableCacheVisualization = true；

                // Disable the application idle detection by setting the UserIdleDetectionMode property of the
                // application's PhoneApplicationService object to Disabled.
                //  注意: 仅在调试模式下使用此设置。禁用用户空闲检测的应用程序在用户不使用电话时将继续运行
                // 并且消耗电池电量。
                PhoneApplicationService.Current.UserIdleDetectionMode = IdleDetectionMode.Disabled;
            }

            //设置当前语言均为美国，用来展示微软的广告
            CultureInfo info = new CultureInfo("en-us");//en-us;.....
            Thread.CurrentThread.CurrentCulture = info;
            Thread.CurrentThread.CurrentUICulture = info;

            UmengSDK.UmengAnalytics.Init("5294adef56240b8fc2242569", "CN");
            UmengSDK.UmengAnalytics.IsDebug = false;//是否输出调试信息
            UmengSDK.UmengAnalytics.UpdateOnlineParamAsync("5294adef56240b8fc2242569");

            //初始化音效
            LoadSound();

            //初始化配置
            StageManager.Instance.Init();

            StageManager.Instance.LaunchCount++;

            GameSession session = XLiveGameManager.CreateSession("9wNvrGnWjDc3md7sAJEKxbqp");
            XLiveUIManager.Initialize(this, session);
            session.Open();

        }

        private static void LoadSound()
        {
            SoundManager.PreLoadSound(SoundManager.addTool, "Sounds/addTool.wav");
            SoundManager.PreLoadSound(SoundManager.cannotmove, "Sounds/cannotmove.wav");
            SoundManager.PreLoadSound(SoundManager.clickbutton, "Sounds/clickbutton.wav");
            SoundManager.PreLoadSound(SoundManager.ele_diamond, "Sounds/ele_diamond.wav");
            SoundManager.PreLoadSound(SoundManager.ele_et, "Sounds/ele_et.wav");
            SoundManager.PreLoadSound(SoundManager.ele_life, "Sounds/ele_life.wav");
            SoundManager.PreLoadSound(SoundManager.ele_wood, "Sounds/ele_wood.wav");
            SoundManager.PreLoadSound(SoundManager.gameover, "Sounds/gameover.wav");
            SoundManager.PreLoadSound(SoundManager.move, "Sounds/move.wav");
            SoundManager.PreLoadSound(SoundManager.rearrange, "Sounds/rearrange.wav");
            SoundManager.PreLoadSound(SoundManager.usebomb, "Sounds/usebomb.wav");
            SoundManager.PreLoadSound(SoundManager.usewood, "Sounds/usewood.wav");
        }

        // 应用程序启动(例如，从“开始”菜单启动)时执行的代码
        // 此代码在重新激活应用程序时不执行
        private void Application_Launching(object sender, LaunchingEventArgs e)
        {
        }

        // 激活应用程序(置于前台)时执行的代码
        // 此代码在首次启动应用程序时不执行
        private void Application_Activated(object sender, ActivatedEventArgs e)
        {
            if (!e.IsApplicationInstancePreserved)
            {
                //初始化配置
                StageManager.Instance.Init();

                //设置当前语言均为美国，用来展示微软的广告
                CultureInfo info = new CultureInfo("en-us");//en-us;.....
                Thread.CurrentThread.CurrentCulture = info;
                Thread.CurrentThread.CurrentUICulture = info;

                UmengSDK.UmengAnalytics.UpdateOnlineParamAsync("5294adef56240b8fc2242569");

                //初始化音效
                LoadSound();

                //返回主界面
                this.RootFrame.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            }
        }

        // 停用应用程序(发送到后台)时执行的代码
        // 此代码在应用程序关闭时不执行
        private void Application_Deactivated(object sender, DeactivatedEventArgs e)
        {
        }

        // 应用程序关闭(例如，用户点击“后退”)时执行的代码
        // 此代码在停用应用程序时不执行
        private void Application_Closing(object sender, ClosingEventArgs e)
        {
            if (XLiveGameManager.CurrentSession != null && XLiveGameManager.CurrentSession.IsValid)
            {
                XLiveGameManager.CurrentSession.Close();
            }
        }

        // 导航失败时执行的代码
        private void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // 导航已失败；强行进入调试器
                System.Diagnostics.Debugger.Break();
            }
        }

        // 出现未处理的异常时执行的代码
        private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
        {
            if (System.Diagnostics.Debugger.IsAttached)
            {
                // 出现未处理的异常；强行进入调试器
                System.Diagnostics.Debugger.Break();
            }
        }

        #region 电话应用程序初始化

        // 避免双重初始化
        private bool phoneApplicationInitialized = false;

        // 请勿向此方法中添加任何其他代码
        private void InitializePhoneApplication()
        {
            if (phoneApplicationInitialized)
                return;

            // 创建框架但先不将它设置为 RootVisual；这允许初始
            // 屏幕保持活动状态，直到准备呈现应用程序时。
            RootFrame = new PhoneApplicationFrame();
            RootFrame.Navigated += CompleteInitializePhoneApplication;

            // 处理导航故障
            RootFrame.NavigationFailed += RootFrame_NavigationFailed;

            // 确保我们未再次初始化
            phoneApplicationInitialized = true;
        }

        // 请勿向此方法中添加任何其他代码
        private void CompleteInitializePhoneApplication(object sender, NavigationEventArgs e)
        {
            // 设置根视觉效果以允许应用程序呈现
            if (RootVisual != RootFrame)
                RootVisual = RootFrame;

            // 删除此处理程序，因为不再需要它
            RootFrame.Navigated -= CompleteInitializePhoneApplication;
        }

        #endregion
    }
}